package com.ellricky.baselib.ui.student;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.andr.common.http.callback.BaseCallback;
import com.andr.common.http.callback.JOCallback;
import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.mvp.BasePresenter;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.adapter.student.StuManageAdapter;
import com.ellricky.baselib.cache.LocalCacheUtil;
import com.ellricky.baselib.entity.UserBean;
import com.ellricky.baselib.entity.student.StudentList;
import com.ellricky.baselib.entity.student.StudentListEntity;
import com.ellricky.baselib.http.Api;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.http.callback.EListCallback;
import com.ellricky.baselib.listener.StuManageListener;
import com.ellricky.baselib.ui.base.BaseRefreshFragment;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.util.NewEBaseCallback;
import com.ellricky.baselib.util.NewEListCallback;
import com.ellricky.baselib.util.NewJOCallback;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ellricky.baselib.BaseConstant.STU_TYPE_VIP;
import static com.ellricky.baselib.http.Api.API_STUDENT_LIST;

public class StudentFragment extends BaseRefreshFragment<Void, BasePresenter<Void>, StudentList> {

    private String pageType = "";

    private String age = "2";
    private String birthOrder;
    private String hourOrder;
    private String searchKey = "";

    private StuManageListener numListener;

    public static StudentFragment newInstance(String type) {
        StudentFragment fragment = new StudentFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BaseConstant.KEY_PAGE_TYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    public StudentFragment setNumListener(StuManageListener numListener) {
        this.numListener = numListener;
        return this;
    }

    @Override
    public void initUIView(View view) {
        if (getArguments() != null) {
            pageType = getArguments().getString(BaseConstant.KEY_PAGE_TYPE);
        }
        super.initUIView(view);
        hideToolBar();
    }


    @Override
    public void loadDataOnInit() {
        updateFilterParam("0", "0", "0", true, searchKey);
        loadHeaderNum();
    }

    private void loadHeaderNum() {
        String url = EAppUtil.getApiHost();
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("action", getHeadActions());
        paramMap.put("eid", LocalCacheUtil.getUid());
        paramMap.put("school_id", LocalCacheUtil.getSchoolId());
        HttpUtils.getInstance().postFrom(url, paramMap, new NewJOCallback(mContext) {
            @Override
            protected void onSuccess(JSONObject jsonObject, String s) {
                JSONObject realDataJo = jsonObject.optJSONObject("data");
                if (realDataJo != null) {
                    // 会员人数
                    String formalNum = realDataJo.optString("memberNum");
                    //  停课人数
                    String stopNum = realDataJo.optString("stopNum");
                    //  未续费人数
                    String noNum = realDataJo.optString("noNum");
                    //  退费会员
                    String returnNum = realDataJo.optString("returnNum");
                    //  超期会员
                    String chaoNum = realDataJo.optString("chaoNum");
                    if (numListener != null)
                        numListener.updateTabNum(formalNum, stopNum, noNum, returnNum, chaoNum);
                }
            }
        });
    }


    void updateFilterParam(String age, String birthOrder, String hourOrder, boolean init, String searchKey) {
        this.age = age;
        this.birthOrder = birthOrder;
        this.hourOrder = hourOrder;
        this.searchKey = searchKey;
        if (rvView != null)
            rvView.loadContent(true);
        if (init && numListener != null) numListener.resetFilterIcon();
    }


    @Override
    protected NewEBaseCallback getResponseCallback2() {
        return new NewEListCallback<StudentList>(mContext) {

            @Override
            protected void onSuccess(List<StudentList> list, String msg) {
                dataSource = list;
                rvView.onLoadFinish(dataSource, refresh);
            }

            @Override
            protected void onErrorCode(String code, String msg) {
                super.onErrorCode(code, msg);
                rvView.onLoadFinish(null, refresh);
            }
        };
    }

    @Override
    protected BaseCallback getResponseCallback() {
        return null;
    }

    @Override
    protected BaseRecyclerAdapter<StudentList> getRvAdapter() {
        return new StuManageAdapter(mConext);
    }


    @Override
    protected void initParamMap() {
        super.initParamMap();
        paramMap.put("action", getActions());
        paramMap.put("school_id", LocalCacheUtil.getSchoolId());
        paramMap.put("eid", LocalCacheUtil.getUid());
        paramMap.put("sort_age", age);           // 筛选值
        paramMap.put("sort_birthday", birthOrder);// 生日排序，升序0 ,降序 1
        paramMap.put("sort_coursenum", hourOrder);// 课时排序，升序0 ,降序 1
        paramMap.put("search_key", searchKey);
        paramMap.put("member_type", pageType);
    }

    private String getActions() {
        String role = LocalCacheUtil.getCardType();
        switch (role) {
            case BaseConstant.CARD_TEACHER:
                return "teacherMemberList";
            case BaseConstant.CARD_TEACHER_MANAGER:
                return "gzaMemberList";
            case BaseConstant.CARD_PRESIDENT:
                return "headMemberList";
        }
        return "";
    }

    private String getHeadActions() {
        String role = LocalCacheUtil.getCardType();
        switch (role) {
            case BaseConstant.CARD_TEACHER:
                return "teacherMemberNum";
            case BaseConstant.CARD_TEACHER_MANAGER:
                return "gzaMemberNum";
            case BaseConstant.CARD_PRESIDENT:
                return "headMemberNum";
        }
        return "";
    }


    @Override
    protected String getUrlPath() {
        return "";
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        super.onItemClick(adapterView, view, i, l);
        StudentList entity = adapter.get(i);
        EAppUtil.jumpPageWithId(this, StudentDetailActivity.class, entity.getMemberId());
    }
}
